﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quexcel.OpenXML.Interfaces;
using System.Xml;
using System.IO.Packaging;
using System.Text.RegularExpressions;
using Quexcel.OpenXML.Excel.Factory;
using Quexcel.OpenXML.Transaction;
using Quexcel.OpenXML.Excel.Worksheet;
using Quexcel.OpenXML.Excel.SharedString;

namespace Quexcel.OpenXML.Excel.Workbook
{
    internal class Workbook : WorkbookComponent, IWorkbook
    {
        private string workbookName;
        private ExcelConnection connection;
        private IWorkbookReader workbookReader;
        private static IWorkbookWriter workbookWriter;
        private static SharedStringData sharedString;

        internal Workbook(ExcelConnection connection, PackagePart documentPart) : base(documentPart)
        {
            this.connection = connection;
            workbookName = (new Regex("(?<name>[^/]+).xml")).Match(documentPart.Uri.ToString()).Result("${name}");
            workbookReader = this.GetReader();
        }

        #region IWorkbook Members

        public string WorkbookName
        {
            get { return workbookName; }
        }

        public XmlNamespaceManager NameSpaceManager
        {
            get { return this.GetReader().NameSpaceManager; }
        }

        public Quexcel.OpenXML.Excel.Worksheet.Worksheet GetSheet(string sheetName, ExcelTransaction transaction)
        {
            PackagePart sheetPart = workbookReader.GetSheetPart(sheetName);
            Quexcel.OpenXML.Excel.Worksheet.Worksheet worksheet = transaction.GetWorksheet(sheetName);
            if (worksheet == null)
            {
                worksheet = new Quexcel.OpenXML.Excel.Worksheet.Worksheet(this, transaction, sheetPart, sheetName);
                if(transaction.AddWorksheet(worksheet))
                    return worksheet;
            }
            return worksheet;
        }

        public SharedStringData GetSharedString(ExcelTransaction transaction)
        {
            if (sharedString == null)
            {
                PackagePart sharedStringPart = workbookReader.GetSharedStringPart();
                sharedString = new SharedStringData(transaction, this, sharedStringPart);
            }
            return sharedString;
        }

        public Quexcel.OpenXML.Excel.Worksheet.Range GetRange(string rangeName, ExcelTransaction transaction)
        {
            string sheetName = null;
            string range = null;

            PackagePart rangePart = workbookReader.GetRangePart(rangeName, out sheetName, out range);
            Quexcel.OpenXML.Excel.Worksheet.Worksheet worksheet = transaction.GetWorksheet(sheetName);
            if (worksheet == null)
            {
                worksheet = new Quexcel.OpenXML.Excel.Worksheet.Worksheet(this, transaction, rangePart, sheetName);
                transaction.AddWorksheet(worksheet);
            }

            return new Range(this, worksheet, rangeName, range);
        }

        public IWorkbookReader GetReader()
        {
            if (workbookReader == null)
                workbookReader = connection.GetWorkbookReader();
            return workbookReader;
        }

        public IWorkbookWriter GetWriter()
        {
            if (workbookWriter == null)
                workbookWriter = connection.GetWorkbookWriter();
            return workbookWriter;
        }

        public bool HasReader()
        {
            return workbookReader != null;
        }

        public bool HasWriter()
        {
            return workbookWriter != null;
        }

        #endregion

        #region IWorkbook Members

        XmlNamespaceManager IWorkbook.NameSpaceManager
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
